
// the class is some information of player in game
public class Player implements Cloneable {
	
	private int direction, direX, direY;
	int posX, posY;
	char sign;
	
	// operate sign
	static final int MOVE_FRONT = 1;
	static final int MOVE_RIGHT = 2;
	static final int MOVE_BACK = 3;
	static final int MOVE_LEFT = 4;
	static final int TURN_LEFT = 1;
	static final int TURN_RIGHT = 2;

	public Player() {

	}
	
	public Player(char sign) {
		this.sign = sign;
	}
	
	public Player(int posX, int posY, char sign, int direction) {
		this.posX = posX;
		this.posY = posY;
		this.sign = sign;
		this.direction = direction;
	}
	
	/*
	 * the direction and direX, direY is
	 * 8 1 2  (-1, 1) ( 0, 1) ( 1, 1)
	 * 7   3  (-1, 0)         ( 1, 0)
	 * 6 5 4  (-1,-1) ( 0,-1) ( 1,-1)
	 */
	public void setDirection(int direction) throws Exception {
		this.direction = direction;
		switch (direction) {
		case 1:
			direX = 0;
			direY = 1;
			break;
		case 2:
			direX = 1;
			direY = 1;
			break;
		case 3:
			direX = 1;
			direY = 0;
			break;
		case 4:
			direX = 1;
			direY = -1;
			break;
		case 5:
			direX = 0;
			direY = -1;
			break;
		case 6:
			direX = -1;
			direY = -1;
			break;
		case 7:
			direX = -1;
			direY = 0;
			break;
		case 8:
			direX = -1;
			direY = 1;
			break;
		default:
			throw new Exception("direction sign error");
		}
	}
	
	public int getDirection() {
		return direction;
	}

	public void turnLeft() {
		direction--;
		if (direction == 0) {
			direction = 8;
		}
		try {
			setDirection(direction);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void turnRight() {
		direction++;
		if (direction == 9) {
			direction = 1;
		}
		try {
			setDirection(direction);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void moveFront() {
		posX += direX;
		posY += direY;
	}
	
	public void moveLeft() {
		turnLeft();
		turnLeft();
		posX += direX;
		posY += direY;
		turnRight();
		turnRight();
	}
	
	public void moveRight() {
		turnRight();
		turnRight();
		posX += direX;
		posY += direY;
		turnLeft();
		turnLeft();
	}
	
	public void moveBack() {
		posX -= direX;
		posY -= direY;
	}
	
	@Override
	public Player clone() {
		Player res = null;
		try {
			res = (Player) super.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return res;
	}
}